package com.SMP.controller.class_room;

import com.SMP.model.Course;
import com.SMP.model.CourseExcel;
import com.SMP.model.Msg;
import com.SMP.service.class_room.CourseService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CourseController {
    @Autowired
    CourseService courseService;

    /**
     * 增加课程
     * @author WYHY
     * @param course 课程对象
     *
     * */
    @RequestMapping("/addCourse")
    @ResponseBody
    public Msg addCourse(Course course){
        int result=courseService.addCourse(course);
        if (result==0){
            return Msg.fail();
        }else{
            return Msg.success();
        }
    }
    /**
     * 修改课程
     * @author WYHY
     * @param course 课程对象
     *
     * */
    @RequestMapping("/updCourse")
    @ResponseBody
    public Msg updCourse(Course course){
        int result=courseService.updCourse(course);
        if (result==0){
            return Msg.fail();
        }else{
            return Msg.success();
        }
    }
    /**
     * 条件查询课程
     * @author WYHY
     * @param course 课程对象
     *
     * */
    @RequestMapping("/selCourse")
    @ResponseBody
    public Msg selCourse(Course course){
        List<Course> result=courseService.selCourse(course);
        if (result==null){
            return Msg.fail();
        }else{
            return Msg.success().add("course",result);
        }
    }

    /**
     * 删除整张课程表
     * @author WYHY
     * @param claid,grade,classs 班级id，年级，班级
     *
     * */
    @RequestMapping("/delAll")
    @ResponseBody
    public Msg deleteByGC(String claid,String grade,String classs){
        int result=courseService.deleteByGC(claid,grade,classs);
        if (result==0){
            return Msg.fail();
        }else if(result==-1){
            return Msg.success();
        }else{
            return Msg.success();
        }
    }

    /**
     * 删除一节课
     * @author WYHY
     * @param id 年级，班级,星期，课节
     *
     * */
    @RequestMapping("/delCou")
    @ResponseBody
    public Msg deleteByCourse(String id){
        int result=courseService.deleteByCourse(id);
        if (result==0){
            return Msg.fail();
        }else{
            return Msg.success();
        }
    }

    private Configuration configuration = null;

    @SuppressWarnings("deprecation")
    public CourseController() {
        configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
    }

    /**
     * 导出Excel(ftl模板)
     *
     * @throws Exception
     */
    @RequestMapping("/CourseExport")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response,Course course) throws Exception {
//        String dir = request.getSession().getServletContext().getRealPath("/");// 总路径
//        String ticketPath = "Ticket/";
//        String ticketFilePath = dir + ticketPath;
        // 根据条件查询数据，把数据装载到一个list中
        List<Course> list = courseService.selCourse(course);
        List<CourseExcel> lists = new ArrayList<CourseExcel>();
        Map map=new HashMap();
        map.put("list",list);
        configuration.setClassForTemplateLoading(this.getClass(), "/com/SMP/utils");
        //设置异常处理器
        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
        Template t=null;
        try {
            // test.ftl为要装载的模板
            t = configuration.getTemplate("CourseExcel.ftl");
            t.setEncoding("utf-8");
//            File ticketFile = new File(ticketFilePath);
//            if (!ticketFile.exists()) {
//                ticketFile.mkdirs();
//            }
            // 输出文档路径及名称
            File outFile = new File("D:/课程表.xsl");
            Writer out = null;
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
            t.process(map, out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            }

//        return null;
    }

}
